$(document).ready(function() {
    var dataSourceUserLines = new kendo.data.DataSource({
        transport: {
            read: {
                url: appBoschUserLines.crud.read,
                dataType: "json"
            },
            update: {
                url: appBoschUserLines.crud.update,
                dataType: "json"
            },
            parameterMap: function(options, operation) {
                var params = {};
                switch (operation)
                {
                    case 'create':
                        params = {models: kendo.stringify(options.models)};
                    case 'read':
                        break;
                    case 'update':
                        params = {models: kendo.stringify(options.models)};
                        break;
                    case 'destroy':
                        params = {id: options.models[0].id};
                }
                return params;
            }
        },
        batch: true,
        schema: {
            model: {
                id: "id",
                fields: {
                    cat_certifications: {
                        editable: false,
                        nullable: false
                    },
                    area: {
                        editable: false,
                        nullable: false
                    },
                    line: {
                        editable: false,
                        nullable: false
                    },
                    certifications: {
                        editable: false,
                        nullable: false
                    },
                    trainings: {
                        editable: false,
                        nullable: false
                    },
                    assign_date: {
                        type: 'date',
                        editable: false,
                        nullable: false
                    }
                }
            }
        }
    });

    $("#gridUserLines").kendoGrid({
        resizable: true,
        columnMenu: true,
        scrollable: true,
        filterable: true,
        selectable: true,
        navigatable: true,
        sortable: true,
        height: 300,
        dataSource: dataSourceUserLines,
        columns: [
            {
                field: "cat_certifications",
                title: "&nbsp;",
                template: '<i title="#=cat_name#" class="fa fa-2x #=cat_icon#" style="color: #=cat_colour#"></i>',
                width: 50
            },
            {
                field: "area",
                title: "Área"
            },
            {
                field: "line",
                title: "Línea de producción"
            },
            {
                field: "certifications",
                title: "# certificaciones"
            },
            {
                field: "trainings",
                title: "# certificaciones en entrenamiento"
            },
            {
                field: "assign_date",
                title: "Asignación en línea",
                template: $('#assignDateTemplate').html()
            },
            {
                command: [
                    {
                        text: "Ver MH",
                        click: mh
                    }]
            }
        ],
        editable: {
            confirmation: "¿Estas seguro que deseas eliminar al usuario de está linea?",
            mode: "inline"
        }
    });
});

function mh(e) {
    e.preventDefault();
    var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
    var lineId = dataItem.line_id;
    var userId = dataItem.user_id;
    var url = [appBoschUserLines.urlMH, lineId, 0, userId].join('/');
    $(location).attr('href', url);
}

